﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using CommunityServerRest;
using System.Xml.Serialization;
using System.IO;
using System.Web;

namespace CSWriter.Blogs {
	public class BlogPostItem : ICloneable {
		public string Title { get; set; }
		public string Body { get; set; }
		public DateTime Date { get; set; }
		public Attachment Attachment { get; set; }
		public int Id { get; set; }
		public string Name { get; set; }
		public BlogInfo BlogInfo { get; set; }
		public Keywords Keywords { get; set; }
		public BlogPostSettings Settings { get; set; }
		public int Views { get; set; }

		public BlogPostItem() {
			Keywords = new Keywords();
		}
		public BlogPostItem(BlogPost restPost) {
			this.Title = restPost.Title;
			this.Body = restPost.Body;
			this.Date = restPost.Date;
			this.Id = restPost.Id;
			this.BlogInfo = restPost.BlogInfo;
			this.Keywords = restPost.Keywords;
			this.Settings = restPost.Settings;
			this.Views = restPost.Views;
		}
		public BlogPostItem(String title, String body, Attachment attachment): this() {
			Title = title;
			Body = body;
			Attachment = attachment;
		}
		internal BlogPost ToRestPost() {
			BlogPost post = new BlogPost();

			post.Id = Id;
			post.Title = Title;
			post.Name = Name;
			post.Date=Date;
			post.Body=Body;
			post.Settings = Settings;
			post.Keywords = Keywords;
			post.BlogInfo = BlogInfo;
			post.Views = Views;
			return post;
		}

		#region ICloneable Members

		object ICloneable.Clone() {
			return Clone();
		}

		#endregion

		internal BlogPostItem Clone() {
			BlogPostItem o = new BlogPostItem();
			o.Id = Id;
			o.Title = Title;
			o.Name = Name;
			o.Body = Body;
			o.Date = Date;
			return o;
		}

		internal void SaveTo(string resultingPath) {
			XmlSerializer serializer = new XmlSerializer(typeof(BlogPostItem));
			StreamWriter stream = new StreamWriter(resultingPath);
			serializer.Serialize(stream, this);
			stream.Close();
		}
	}
}
